package com.linx.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.domain.dto.RoleDTO;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.satoken.utils.LoginHelper;
import com.linx.system.domain.AppMenu;
import com.linx.system.domain.AppMenuType;
import com.linx.system.domain.bo.AppMenuBo;
import com.linx.system.domain.vo.AppMenuServiceVo;
import com.linx.system.domain.vo.AppMenuVo;
import com.linx.system.mapper.AppMenuMapper;
import com.linx.system.mapper.AppMenuTypeMapper;
import com.linx.system.service.IAppMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 移动端应用菜单Service业务层处理
 *
 * @author linx
 * @date 2024-12-24
 */
@RequiredArgsConstructor
@Service
public class AppMenuServiceImpl implements IAppMenuService {

    private final AppMenuMapper baseMapper;
    private final AppMenuTypeMapper appMenuTypeMapper;

    /**
     * 查询移动端应用菜单
     *
     * @param appMenuId 主键
     * @return 移动端应用菜单
     */
    @Override
    public AppMenuVo queryById(Long appMenuId) {
        return baseMapper.selectVoById(appMenuId);
    }

    /**
     * 分页查询移动端应用菜单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 移动端应用菜单分页列表
     */
    @Override
    public TableDataInfo<AppMenuVo> queryPageList(AppMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppMenu> lqw = buildQueryWrapper(bo);
        Page<AppMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的移动端应用菜单列表
     *
     * @param bo 查询条件
     * @return 移动端应用菜单列表
     */
    @Override
    public List<AppMenuVo> queryList(AppMenuBo bo) {
        LambdaQueryWrapper<AppMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppMenu> buildQueryWrapper(AppMenuBo bo) {
        LambdaQueryWrapper<AppMenu> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getServiceKey()), AppMenu::getServiceKey, bo.getServiceKey());
        lqw.like(StringUtils.isNotBlank(bo.getServiceName()), AppMenu::getServiceName, bo.getServiceName());
        return lqw;
    }

    /**
     * 新增移动端应用菜单
     *
     * @param bo 移动端应用菜单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppMenuBo bo) {
        AppMenu add = MapstructUtils.convert(bo, AppMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAppMenuId(add.getAppMenuId());
        }
        return flag;
    }

    /**
     * 修改移动端应用菜单
     *
     * @param bo 移动端应用菜单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppMenuBo bo) {
        AppMenu update = MapstructUtils.convert(bo, AppMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppMenu entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除移动端应用菜单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<AppMenuServiceVo> searchAppMenuServiceList() {
        List<AppMenuServiceVo> list = new ArrayList<>();
        //查询常用服务
        AppMenuServiceVo appMenuServiceVo = new AppMenuServiceVo();
        appMenuServiceVo.setAppMenuTypeName("常用功能");
        List<RoleDTO> roles = LoginHelper.getLoginUser().getRoles();
        List<Long> roleIdList=new ArrayList<>();
        if (CollUtil.isNotEmpty(roles)) {
           roleIdList=  roles.stream()
                .map(RoleDTO::getRoleId)
                .collect(Collectors.toList());



        }
        appMenuServiceVo.setAppMenuVoList(baseMapper.selectAppMenuServiceListByUserIdAndRoleIds(LoginHelper.getUserId(),
            roleIdList));
        list.add(appMenuServiceVo);
        //查询分类下的列表
        QueryWrapper<AppMenuType> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("update_time"); // 升序
        List<AppMenuType> appMenuTypeList = appMenuTypeMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(appMenuTypeList)) {
            for (int i = 0; i < appMenuTypeList.size(); i++) {
                AppMenuServiceVo appMenuServiceVoQt = new AppMenuServiceVo();
                List<AppMenuVo> appMenuVos =
                    baseMapper.selectAppMenuServiceListByRoleIds(appMenuTypeList.get(i).getAppMenuTypeId(), roleIdList);
                appMenuServiceVoQt.setAppMenuTypeId(appMenuTypeList.get(i).getAppMenuTypeId());
                appMenuServiceVoQt.setAppMenuTypeName(appMenuTypeList.get(i).getMenuTypeName());
                appMenuServiceVoQt.setAppMenuVoList(appMenuVos);
                list.add(appMenuServiceVoQt);
            }

        }
        return list;
    }
}
